package ops.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ops.controller.ControllerManager;
import ops.controller.TeacherController;
import ops.i18n.Resources;
import ops.util.ViewHelper;
import ops.view.forms.Form;
import ops.view.forms.component.CheckboxInput;
import ops.view.forms.component.LimitedSizeInput;
import ops.view.forms.component.NumberInput;
import ops.view.tables.Column;
import ops.view.tables.TableMaker;

public class TeacherRegisterPanel extends JPanel implements Refreshable {

	private TeacherController teacherController = ControllerManager.getInstance().getTeacherController();
	
	private JPanel formPanel;

	private JPanel buttonsPanel;
	private JButton newButton;
	private JButton saveButton;
	private JButton removeButton;
	private JButton cancelButton;

	private TableMaker tableMaker;
	private Form form;

	public TeacherRegisterPanel() {
		super(new BorderLayout());

		init();
	}

	public void clearTable() {
		tableMaker.clearTable();
	}
	
	public void addRowToTable(Object... data) {
		tableMaker.addRow(data);
	}

	private void init() {
		formPanel = new JPanel(new BorderLayout());

		initButtons();
		initForm();
		initTable();

		add(formPanel, BorderLayout.SOUTH);
	}

	private void initTable() {
		Resources resources = Resources.getInstance();

		tableMaker = new TableMaker();
		tableMaker.addColumn(new Column(resources.getString("teacher.code"), Long.class, 15));
		tableMaker.addColumn(new Column(resources.getString("name"), String.class, 70));
		tableMaker.addColumn(new Column(resources.getString("email"), String.class));
		tableMaker.addColumn(new Column(resources.getString("exclusive"), Boolean.class));
		tableMaker.addColumn(new Column(resources.getString("hierarchy"), String.class));
		tableMaker.addColumn(new Column(resources.getString("subordination"), String.class));
		tableMaker.setControlButtons(saveButton, removeButton);
		
		tableMaker.addSelectionListener(new SelectionListener());
		
		add(ViewHelper.makeScrollPane(tableMaker.getTable()), BorderLayout.CENTER);	
	}
	
	private void initButtons() {
		buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));

		ButtonListener listener = new ButtonListener();

		removeButton = ViewHelper.makeRemoveButton();
		removeButton.addActionListener(listener);
		saveButton = ViewHelper.makeSaveButton();
		saveButton.addActionListener(listener);
		newButton = ViewHelper.makeNewButton();
		newButton.addActionListener(listener);
		cancelButton = ViewHelper.makeCancelButton();
		cancelButton.addActionListener(listener);

		buttonsPanel.add(newButton);
		buttonsPanel.add(saveButton);
		buttonsPanel.add(removeButton);
		buttonsPanel.add(cancelButton);

		formPanel.add(buttonsPanel, BorderLayout.CENTER);
	}

	private void initForm() {
		Resources resources = Resources.getInstance();

		form = new Form();
		form.addInput("teacher.code", new NumberInput(resources.getString("teacher.code")+":", 20, true, false));
		form.addInput("teacher.name", new LimitedSizeInput(resources.getString("name")+":", 40, true));
		form.addInput("teacher.email", new LimitedSizeInput(resources.getString("email")+":", 40, true));
		form.addInput("teacher.exclusive", new CheckboxInput(resources.getString("exclusive")+":"));
		form.addInput("teacher.hierarchy", new LimitedSizeInput(resources.getString("hierarchy")+":", 20, true));
		form.addInput("teacher.subordination", new LimitedSizeInput(resources.getString("subordination")+":", 20, true));

		formPanel.add(form.getPanel(), BorderLayout.SOUTH);
	}
	
	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (!(e.getSource() instanceof JButton)) {
				return;
			}
			
			JButton button = (JButton) e.getSource();
			
			if (!button.isEnabled()) {
				return;
			}

			Long code = form.getLongFor("teacher.code");
			String name = form.getStringFor("teacher.name");
			String email = form.getStringFor("teacher.email");
			Boolean exclusive = form.getBooleanFor("teacher.exclusive");
			String hierarchy = form.getStringFor("teacher.hierarchy");
			String subordination = form.getStringFor("teacher.subordination");
			
			if (button == saveButton) {
				if (form.validate()) {				
					teacherController.saveTeacher(code, name, email, exclusive, hierarchy, subordination);
					
					saveButton.setEnabled(false);
					newButton.setEnabled(true);
					
					form.clear();
					teacherController.endAddProcess();
				} else {
					ViewHelper.showFormValidationError();
					return;
				}
			} else if (button == removeButton) {
				teacherController.removeTeacher(code);
			} else if (button == newButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(true);
				newButton.setEnabled(false);
				
				teacherController.startAddProcess();
			} else if (button == cancelButton) {
				form.clear();
				tableMaker.clearSelection();
				
				saveButton.setEnabled(false);
				newButton.setEnabled(true);
				
				teacherController.endAddProcess();
			}
		}
	}
	
	private class SelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent e) {
			if (e.getValueIsAdjusting()) {
				return;
			}
			
			ListSelectionModel lsm = (ListSelectionModel) e.getSource();

			if (!lsm.isSelectionEmpty()) {
				Long code = (Long) tableMaker.getValueAt(tableMaker.getSeletedRow(), 0);
				String name = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 1);
				String email = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 2);
				Boolean exclusive = (Boolean) tableMaker.getValueAt(tableMaker.getSeletedRow(), 3);
				String hierarchy = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 4);
				String subordination = (String) tableMaker.getValueAt(tableMaker.getSeletedRow(), 5);

				form.setValue("teacher.code", code);
				form.setValue("teacher.name", name);
				form.setValue("teacher.email", email);
				form.setValue("teacher.exclusive", exclusive);
				form.setValue("teacher.hierarchy", hierarchy);
				form.setValue("teacher.subordination", subordination);

			} else {
				form.clear();
			}
		}		
	}

	public void refresh() {
		teacherController.updateTable();
	}

}
